//
//  RCDAlertView.m
//  SchoolHelper
//
//  Created by  任辰东 on 2020/4/7.
//  Copyright © 2020 JSY. All rights reserved.
//

#import "RCDAlertView.h"
#import "UILabel+LineSpace.h"

#define BtnWidth ((kScreenWidth - 80*kScreen_Ratio_W)/2.0)

@interface RCDAlertView ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UIButton *cancleBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UIView *line;

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) id subTitle;
@property (nonatomic, copy) NSString *cancel;
@property (nonatomic, copy) NSString *confirm;

@end

@implementation RCDAlertView

-(instancetype)initWithTile:(NSString *)title subTitle:(id)subTitle cancelTitle:(NSString *)cancel confirmTitle:(NSString *)confirm
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        _title = title;
        _subTitle = subTitle;
        _cancel = cancel;
        _confirm = confirm;
        [self setupViews];
        [self setupLayout];
    }
    return self;
}

- (void)setupViews
{
    [self addSubview:self.containerView];
    [self.containerView addSubview:self.titleLabel];
    [self.containerView addSubview:self.subTitleLabel];
    [self.containerView addSubview:self.line];
    if (!IsEmpty(self.cancel) && !IsEmpty(self.confirm)) {
        [self.containerView addSubview:self.cancleBtn];
        [self.containerView addSubview:self.sureBtn];
        [self.sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.cancleBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.cancleBtn setTitle:self.cancel forState:UIControlStateNormal];
        [self.sureBtn setTitle:self.confirm forState:UIControlStateNormal];
        
    }else if (!IsEmpty(self.cancel)){
        [self.containerView addSubview:self.cancleBtn];

        [self.cancleBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.cancleBtn setTitle:self.cancel forState:UIControlStateNormal];

    }else{
        [self.containerView addSubview:self.sureBtn];
        [self.sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.sureBtn setTitle:self.confirm forState:UIControlStateNormal];
    }
    
    self.titleLabel.text = self.title;
    if ([self.subTitle isKindOfClass:[NSAttributedString class]]) {
        
        self.subTitleLabel.attributedText = self.subTitle;
        
    }else{
        
        [self.subTitleLabel setText:self.subTitle lineSpacing:5];
    }
}

- (void)setupLayout
{
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left).offset(40 *kScreen_Ratio_W);
        make.right.equalTo(self.mas_right).offset(-40 *kScreen_Ratio_W);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.containerView).offset(0);
        make.right.equalTo(self.containerView).offset(0);
        make.top.equalTo(self.containerView).offset(0);
        make.height.mas_equalTo(50);
    }];
    
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.containerView).offset(15);
        make.right.equalTo(self.containerView).offset(-15);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(25);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.containerView);
        if (IsEmpty(self.subTitle)) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(30);
        }else{
            make.top.equalTo(self.subTitleLabel.mas_bottom).offset(30);
        }

        make.height.mas_equalTo(kOnePixel);
    }];
    
    if (!IsEmpty(self.cancel) && !IsEmpty(self.confirm)) {
        
        [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.containerView).offset(0);
            make.top.equalTo(self.line.mas_bottom).offset(0);
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(BtnWidth);
            make.bottom.equalTo(self.containerView.mas_bottom).offset(0);
        }];
        
        
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(self.containerView.mas_right).offset(0);
            make.top.equalTo(self.line.mas_bottom).offset(0);
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(BtnWidth);
            make.bottom.equalTo(self.containerView.mas_bottom).offset(0);
        }];
        
    }else if (!IsEmpty(self.cancel)){
        [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.containerView).offset(0);
            make.top.equalTo(self.line.mas_bottom).offset(0);
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(BtnWidth*2);
            make.bottom.equalTo(self.containerView.mas_bottom).offset(0);
        }];
    }else{
        
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(self.containerView.mas_right).offset(0);
            make.top.equalTo(self.line.mas_bottom).offset(0);
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(BtnWidth*2);
            make.bottom.equalTo(self.containerView.mas_bottom).offset(0);
        }];
    }
    
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
    self.subTitleLabel.textAlignment = textAlignment;
}

- (void)sureBtnAction
{
    if (self.didConfirm) {
        self.didConfirm();
    }
    [self dismiss];
}

- (void)cancelBtnAction
{
    if (self.didCancel) {
        self.didCancel();
    }
    [self dismiss];
}

-(void)dismiss
{
    [self removeFromSuperview];
}
- (void)show
{
    [kKeyWindow addSubview:self];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(kKeyWindow);
    }];
    
    CAAnimation *showsAnimation = [self defaultShowsAnimation];
    [self.containerView.layer addAnimation:showsAnimation forKey:@"Popup"];
}

#pragma mark- Show Animations
#define transformScale(scale) [NSValue valueWithCATransform3D:[self transform3DScale:scale]]

- (CATransform3D)transform3DScale:(CGFloat)scale
{
    // Add scale on current transform.
    CATransform3D currentTransfrom = CATransform3DScale(self.layer.transform, scale, scale, 1.0f);
    
    return currentTransfrom;
}

- (CAAnimation *)defaultShowsAnimation
{
    NSArray *frameValues = @[transformScale(1.0f), transformScale(1.0f), transformScale(1.0f), transformScale(1.0f)];
    NSArray *frameTimes = @[@(1.0f), @(1.0f), @(1.0f), @(1.0f)];
    return [self animationWithValues:frameValues times:frameTimes duration:0.3f];
}

- (CAKeyframeAnimation *)animationWithValues:(NSArray*)values times:(NSArray*)times duration:(CGFloat)duration {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    [animation setValues:values];
    [animation setKeyTimes:times];
    [animation setFillMode:kCAFillModeForwards];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    [animation setRemovedOnCompletion:NO];
    [animation setDuration:duration];
    
    return animation;
}

#pragma mark -- lazyLoad

-(UIView *)containerView
{
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = UIColorFromHex(0xffffff);
        _containerView.layer.cornerRadius = 5;
        _containerView.clipsToBounds = YES;
    }
    return _containerView;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = UIColorFromHex(kColorBG);
        _titleLabel.textColor = UIColorFromHex(0x333333);
        _titleLabel.font = UIBoldFontSize(18);
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

-(UILabel *)subTitleLabel
{
    if (!_subTitleLabel) {
        _subTitleLabel = [UILabel new];
        _subTitleLabel.textAlignment = NSTextAlignmentCenter;
        _subTitleLabel.textColor = UIColorFromHex(0x333333);
        _subTitleLabel.font = UIFontSize(16);
        _subTitleLabel.numberOfLines = 0;
    }
    return _subTitleLabel;
}

-(UIButton *)cancleBtn
{
    if (!_cancleBtn) {
        _cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleBtn setBackgroundColor:UIColorFromHex(0xffffff)];
        [_cancleBtn setTitleColor:UIColorFromHex(0x666666) forState:UIControlStateNormal];
        _cancleBtn.titleLabel.font = UIFontSize(16);
    }
    return _cancleBtn;
}

-(UIButton *)sureBtn
{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确认提交" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:UIColorFromHex(0xffffff) forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = UIFontSize(16);
        [_sureBtn setBackgroundColor:UIColorFromHex(kColorMain)];
    }
    return _sureBtn;
}

-(UIView *)line
{
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = UIColorFromHex(kColorLine);
    }
    return _line;
}

@end
